<template>
  <div class="role-manage">
    <div class="query-form">
      <el-form ref="form" :inline="true" :model="queryForm">
        <el-form-item label="名称" prop="year">
          <el-input v-model="queryForm.title" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getMouthTypeList">查询</el-button>
          <el-button @click="handleReset('form')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="base-table">
      <div class="action">
        <el-button type="primary" @click="handleAdd">创建</el-button>
      </div>
      <el-table :data="roleList">
        <el-table-column
            v-for="item in columns"
            :key="item.prop"
            :prop="item.prop"
            :label="item.label"
            :width="item.width"
            :formatter="item.formatter"
        >
        </el-table-column>
        <el-table-column label="操作" width="360">
          <template #default="scope">
<!--            <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>-->
            <el-button size="mini" @click="handleDetail(scope.row)">详情</el-button>
            <el-button size="mini" type="danger" @click="handleDel(scope.row.id)">删除</el-button>
<!--            <el-button size="mini" type="primary" @click="handleSalaryStructure(scope.row)">设置工资结构</el-button>
            <el-button size="mini" type="success" @click="handleGenerate(scope.row.id)">生成名单</el-button>-->
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          class="pagination"
          background
          layout="prev, pager, next"
          :total="pager.total"
          :page-size="pager.pageSize"
          @current-change="handleCurrentChange"
      />
    </div>
    <el-dialog title="新增" v-model="showModal">
      <el-form
          ref="dialogForm"
          :model="salaryForm"
          label-width="100px"
          :rules="rules">

        <el-form-item label="年月" prop="time">
          <el-date-picker
              @change="selectChangedTime"
              v-model="salaryForm.time"
              type="month"
              placeholder="选择年月">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="选择字段">
          <el-checkbox
              :indeterminate="isIndeterminate"
              v-model="checkAll"
              @change="handleCheckAllChange"
          >全选</el-checkbox>
          <el-checkbox-group
              v-model="checkedSalaryFields"
              @change="handleSalaryFieldsChange">
            <el-checkbox
                v-for="fieldDesc in salaryFieldOptions"
                :label="fieldDesc"
                :key="fieldDesc">
              {{fieldDesc}}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>

      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="handleClose">取 消</el-button>
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
        </span>
      </template>
    </el-dialog>

    <el-dialog title="详情" v-model="showDetailModal" @close="closeDetailDialog">
      <el-form
          ref="dialogForm"
          :model="salaryForm"
          label-width="100px"
          :rules="rules">

        <el-form-item label="年月" prop="time">
          <el-date-picker
              @change="selectChangedTime"
              v-model="salaryForm.time"
              type="month"
              placeholder="选择年月"
              disabled
          >
          </el-date-picker>
        </el-form-item>

        <el-form-item label="选择字段">
          <el-checkbox
              :indeterminate="isIndeterminate"
              v-model="checkAll"
              @change="handleCheckAllChange"
              disabled
          >全选</el-checkbox>
          <el-checkbox-group
              v-model="checkedSalaryFields"
              @change="handleSalaryFieldsChange">
            <el-checkbox
                v-for="fieldDesc in salaryFieldOptions"
                :label="fieldDesc"
                :key="fieldDesc"
                disabled>
              {{fieldDesc}}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import utils from "../utils/utils";
export default {
  name: "role",
  data() {
    return {
      queryForm: {
        title: "",
      },
      columns: [
        {
          label: "序号",
          prop: "id",
          width: 250
        },
        {
          label: "时间",
          prop: "time",
        },
        {
          label: "名称",
          prop: 'title',
        },
        {
          label: "表名",
          prop: 'tableName',
        },
        {
          label: "创建者",
          prop: "createBy",
        },
        {
          label: "创建时间",
          prop: "createTime",
          width: 150,
          formatter(row, column, value) {
            return utils.formateDate(new Date(value));
          },
        },
        {
          label: "更新者",
          prop: "updateBy",
        },
        {
          label: "更新时间",
          prop: "updateTime",
          width: 150,
          formatter(row, column, value) {
            return utils.formateDate(new Date(value));
          },
        },

      ],
      roleList: [],
      pager: {
        total: 0,
        pageNum: 1,
        pageSize: 10,
      },
      showModal: false,
      showDetailModal: false,
      action: "create",
      salaryForm: {},
      rules: {
        year: [
          {
            required: true,
            message: "请输入年份",
          },
        ],
        mouth: [
          {
            required: true,
            message: "请选择月份",
          },
        ],
        time: [
          {
            required: true,
            message: "请选择年月",
          },
        ],
      },

      salaryFieldList: [], // 后台获取的数据
      salaryFieldOptions: [], // 修改后的数据
      checkAll: false,
      checkedSalaryFields: [], // 选中的数据
      isIndeterminate: true,
    };
  },
  mounted() {
    this.getMouthTypeList();
    this.getSalaryFieldAll();
  },
  methods: {
    // 工资结构列表初始化
    async getMouthTypeList() {
      let param = {
        page: this.pager.pageNum,
        pageSize: this.pager.pageSize,
        title: this.queryForm.title
      }
      let list= await this.$api.getMouthTypeList(param);
      debugger
      this.roleList = list.rows;
      this.pager.total = list.records;
    },
    // 初始化获取所有工资类型字段
    async getSalaryFieldAll() {
      // debugger
      let list= await this.$api.getSalaryFieldAll();
      //console.log(list)
      this.salaryFieldList = list;
      const res = list.map(function (item) {
        return item.fieldDesc;
      })

      this.salaryFieldOptions = res;
    },
    // 表单重置
    handleReset(form) {
      this.queryForm.title = "";
      this.$refs[form].resetFields();
    },
    // 工资结构添加
    handleAdd() {
      this.action = "create";
      this.showModal = true;
      this.getSalaryFieldAll();
    },
    // 工资结构编辑
    handleEdit(row) {
      this.action = "edit";
      this.showModal = true;
      this.$nextTick(() => {
        this.salaryForm = {
          id: row.id,
          time: row.time,
          mouth: row.mouth,
        };
      });
    },
    async handleDetail(row) {
      //debugger
      this.getSalaryFieldAll()
      let list = await this.$api.findMonthTypeById(row.id);
      const res = list.map(function (item) {
        return item.fieldDesc;
      })
      this.checkedSalaryFields = res;

      this.action = "detail";
      this.showDetailModal = true;
      this.$nextTick(() => {
        this.salaryForm = {
          id: row.id,
          time: row.time
        };
      });
    },
    closeDetailDialog(){
      this.handleReset("dialogForm");
      this.salaryFieldOptions = []
    },
    // 工资结构删除
    async handleDel(id) {
      await this.$api.deleteMouthType(id);
      this.$message.success("删除成功");
      this.getMouthTypeList();
    },
    // 弹框关闭
    handleClose() {
      this.handleReset("dialogForm");
      this.showModal = false;
      this.salaryFieldOptions = []
    },
    async selectChangedTime(val) {
      const year = val.getFullYear();
      const month = val.getMonth() + 1;
      if(month < 10){
        const time = year+'-0'+month;
        this.salaryForm.time = time;
      }else{
        const time = year+'-'+month;
        this.salaryForm.time = time;
      }

    },
    // 工资结构提交
    handleSubmit() {
      this.$refs.dialogForm.validate(async (valid) => {
        if (valid) {
          //debugger

          // 获取选中复选框的数据的id
          let that = this;
          let ids = [];
          that.checkedSalaryFields.map(function (item1) {
            that.salaryFieldList.map(function (item2) {
              if(item1 === item2.fieldDesc) {
                ids.push(item2.id)
              }
            })
          })
          this.salaryForm.ids = ids

          let { salaryForm, action } = this;
          let params = { ...salaryForm, action };

          if(this.action == "create") {
            await this.$api.addMouthType(params);
            this.$message.success("创建成功");
          } else if(this.action == "edit") {
            await this.$api.updateMouthType(params);
            this.$message.success("修改成功");
          }
          this.showModal = false;
          this.handleReset("dialogForm");
          this.getMouthTypeList();
        }
      });
    },
    // 设置工资结构
    // handleSalaryStructure(row) {
    //   this.$router.push({
    //     path: `/SalaryStructure`,
    //     query: { id : row.id, year: row.year, month: row.mouth },
    //   })
    //   // this.$router.push({
    //   //   path: `/sub-category/${row.id}/list`,
    //   //   query: { categoryName: row.name },
    //   // })
    // },
    // 生成名单
    // async handleGenerate(id) {
    //   await this.$api.generate(id);
    //   this.$message.success("生成名单成功");
    // },
    handleCurrentChange(current) {
      this.pager.pageNum = current;
      this.getMouthTypeList();
    },

    handleCheckAllChange(val) {
      this.checkedSalaryFields = val ? this.salaryFieldOptions : []
      this.isIndeterminate = false
    },
    handleSalaryFieldsChange(value) {
      let checkedCount = value.length
      this.checkAll = checkedCount === this.salaryFieldOptions.length
      this.isIndeterminate =
          checkedCount > 0 && checkedCount < this.salaryFieldOptions.length
    },
  },
};
</script>

<style lang="scss">
</style>